package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.model.QsgJoin;
import com.wuming.caiwu.service.QsgJoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/join")
public class QsgJoinController extends BaseController {

    @Autowired
    private QsgJoinService joinService;

    @RequestMapping(value = "/manageJoin", method = RequestMethod.GET)
    public String manageJoin() {
        return "/join/manageJoin";
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) String startDate,
                             @RequestParam(required = false) String endDate,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<QsgJoin> joins = joinService.getQsgJoinByPage(startDate,endDate,pageIndex, pageSize);
            int total = joinService.getTotal(startDate,endDate);
            return CommonResult.createSuccessResult(joins, total, "查询加盟商信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询加盟商信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询加盟商信息失败.");
        }
    }

}
